package com.magist.manage.web.facade.impl;

import com.magist.core.constants.Deleted;
import com.magist.core.enums.SysMenuEnum;
import com.magist.manage.web.entity.SysMenu;
import com.magist.manage.web.entity.UserAuth;
import com.magist.manage.web.facade.IndexFacade;
import com.magist.manage.web.service.SysMenuService;
import com.magist.manage.web.service.UserAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class IndexFacadeImpl implements IndexFacade {

    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private UserAuthService userAuthService;

    @Override
    public String getUserMenuString(String id) {
        Assert.hasLength(id, "用户ID为空");
        UserAuth userAuth = userAuthService.getById(id);
        String root = "";
        if(ObjectUtils.isEmpty(userAuth)) {
            return root;
        }
        String menuIds = userAuth.getRouterNameGroup();
        SysMenu param = new SysMenu();
        param.setDeleted(Deleted.NO_STR);
        List<SysMenu> allMenu = sysMenuService.find(param);
        List<SysMenu> list = allMenu.stream()
                .filter((SysMenu b) -> menuIds.contains(b.getId()))
                .collect(Collectors.toList());
        //菜单MAP
        Map<String, SysMenu> menuMap = allMenu.stream()
                .filter((SysMenu b) -> !b.getType().equals(SysMenuEnum.BUTTON.getCode()))
                .collect(Collectors.toMap(SysMenu::getId, sysMenu -> sysMenu));

        for (SysMenu rm : list) {
            if (!rm.getParentId().equals("0")) {
                SysMenu pMenu = menuMap.get(rm.getParentId());
                if(!ObjectUtils.isEmpty(pMenu)) {
                    if (pMenu.getParentId() !=null && !pMenu.getParentId().equals("0")) {
                        SysMenu ppMenu = menuMap.get(pMenu.getParentId());
                        if (ppMenu != null) {
                            if (root.length() > 0) {
                                if (!root.contains(ppMenu.getRouterName()))
                                    root = root + "#" + ppMenu.getRouterName();
                            } else {
                                root = ppMenu.getRouterName();
                            }
                        }
                    }
                    if (root.length() > 0) {
                        if (!root.contains(pMenu.getRouterName()))
                            root = root + "#" + pMenu.getRouterName();
                    }else {
                        root = pMenu.getRouterName();
                    }
                }

            }
            if (root.length() > 0) {
                if (!root.contains(rm.getRouterName()))
                    root = root + "#" + rm.getRouterName();
            } else {
                root = rm.getRouterName();
            }
        }

        return root;
    }

    @Override
    public String getUserMenuOpString(String id) {
        Assert.hasLength(id, "用户ID为空");
        UserAuth userAuth = userAuthService.getById(id);
        String root = "";
        if(ObjectUtils.isEmpty(userAuth)) {
            return root;
        }
        String menuIds = userAuth.getRouterNameGroup();
        SysMenu param = new SysMenu();
        param.setDeleted(Deleted.NO_STR);
        List<SysMenu> allMenu = sysMenuService.find(param);
        List<SysMenu> opList = allMenu.stream()
                .filter((SysMenu b) -> menuIds.contains(b.getId()) && b.getType().equals(SysMenuEnum.BUTTON.getCode()))
                .collect(Collectors.toList());

        for (SysMenu rm : opList) {
            if (root.length() > 0) {
                root = root + "#" + rm.getRouterName();
            } else {
                root = rm.getRouterName();
            }
        }

        return root;
    }
}
